import { ORDER_TYPE, DELIVERY_TYPE_ENUM, ORDER_STATUS, AFTER_ORDER_STATUS, ORDER_AFTER_SALE_TYPE } from '../../constants/order';
const { compareTimeValue } = require('../../common/utils');

/**
 * 处理订单状态信息
 * @param {Object} orderInfo 订单信息
 */
export const handleOrderStatus = (orderInfo = {}) => {
    const { orderStatus, deliveryMode, afterSaleDeadlineAt, orderType } = orderInfo;
    let text = '订单状态异常';
    let tip = '订单状态异常，请刷新后尝试';
    let iconType = ''; // 状态展示图标
    let isHavePay = false; // 是否有支付
    let isHaveListCancel = false; // 列表是否有取消
    let isHaveCancel = false; // 是否有取消
    let isHaveAfterSale = false; // 是否能够售后
    let isHaveActionBar = false; // 是否存在操作栏
    let isHaveExpressInfo = false; // 是否存在快递信息
    let isHavePickUpInfo = false; // 是否存在自提信息
    let isHavePickUpCode = false; // 是否存在自提码
    let isHaveConfirm = false; // 是否有确认收货
    let isHaveContactBrand = false; // 是否有联系商家
    let isPickUp = deliveryMode === DELIVERY_TYPE_ENUM.self; // 是否是自提
    let isDayExpress = deliveryMode === DELIVERY_TYPE_ENUM.city; // 是否是当日达
    let isExpress = deliveryMode === DELIVERY_TYPE_ENUM.express; // 是否是快递
    switch (orderStatus) {
        case ORDER_STATUS.ORDER_CREATED:
            text = '订单已创建';
            tip = '请在10分钟内完成付款';
            isHaveCancel = orderType === ORDER_TYPE.SALE;
            isHaveListCancel = orderType === ORDER_TYPE.SALE;
            iconType = 'pay';
            isHavePay = true;
            isHaveActionBar = true;
            break;
        case ORDER_STATUS.ORDER_TO_PAY:
            text = '待付款';
            tip = '请在货物送达后完成货到付款';
            iconType = 'pay';
            break;
        case ORDER_STATUS.ORDER_NEED_PAY:
            text = '待付款';
            tip = '请在10分钟内完成付款';
            isHaveCancel = orderType === ORDER_TYPE.SALE;
            isHaveListCancel = orderType === ORDER_TYPE.SALE;
            iconType = 'pay';
            isHavePay = true;
            isHaveActionBar = true;
            break;
        case ORDER_STATUS.ORDER_PAYED:
            text = isExpress ? '待发货' : '待备货';
            tip = isExpress ? '请耐心等待商家发货' : '商家正在备货中，请等待备货完成';
            isHaveCancel = orderType === ORDER_TYPE.SALE;
            isHaveExpressInfo = isExpress;
            isHavePickUpInfo = isPickUp;
            isHaveContactBrand = true;
            iconType = 'pack';
            break;
        case ORDER_STATUS.ORDER_DELIVERED:
            text = '待收货';
            tip = '商家已发货，15天后将自动签收';
            iconType = 'package';
            isHaveConfirm = true;
            isHaveActionBar = true;
            isHaveAfterSale = orderType === ORDER_TYPE.SALE;
            isHaveExpressInfo = true;
            isHaveContactBrand = true;
            break;
        case ORDER_STATUS.ORDER_PREPARED:
            text = '待自提';
            tip = '商品已备货完成，请出示取货码自提';
            iconType = 'pickup';
            isHavePickUpInfo = true;
            isHaveActionBar = true;
            isHavePickUpCode = true;
            isHaveAfterSale = orderType === ORDER_TYPE.SALE;
            isHaveConfirm = true;
            isHaveContactBrand = true;
            break;
        case ORDER_STATUS.ORDER_CANCELLING:
            text = '取消申请中';
            tip = '请等待商家审核';
            iconType = 'cancelling';
            isHavePickUpInfo = isPickUp;
            isHaveExpressInfo = isExpress;
            break;
        case ORDER_STATUS.ORDER_COMPLETE:
            text = '已完成';
            tip = '感谢您的支持，期待下次光临';
            iconType = 'complete';
            isHaveAfterSale = orderType === ORDER_TYPE.SALE && afterSaleDeadlineAt ? compareTimeValue(afterSaleDeadlineAt, new Date()) : false;
            isHavePickUpInfo = isPickUp;
            isHaveExpressInfo = isExpress;
            break;
        case ORDER_STATUS.ORDER_CLOSE_BEFORE:
        case ORDER_STATUS.ORDER_CLOSE_AFTER:
            text = '订单已关闭';
            tip = '交易取消，已关闭订单';
            iconType = 'close';
            break;
    }
    return {
        text,
        tip,
        iconType,
        isHavePay,
        isHaveCancel,
        isHaveListCancel,
        isHaveAfterSale,
        isHaveActionBar,
        isHaveExpressInfo,
        isHavePickUpInfo,
        isHavePickUpCode,
        isHaveConfirm,
        isHaveContactBrand,
        isPickUp,
        isDayExpress,
        isExpress,
    };
};

/**
 * 处理售后订单状态信息
 * @param {Object} orderInfo 订单详情
 */
export const handleAfterSaleOrderStatus = (orderInfo) => {
    const { afterSaleStatus, type, cancelled } = orderInfo;
    let text = '订单状态异常';
    let tip = '订单状态异常，请刷新后尝试';
    let isHaveCancel = false;
    let isHaveExpress = false;
    let isShowExpress = false;
    let isHaveRefuseReason = false;
    const isOnlyRefund = type === ORDER_AFTER_SALE_TYPE.onlyRefund;
    switch (afterSaleStatus) {
    case AFTER_ORDER_STATUS.APPLYING: // 申请中
        text = isOnlyRefund ? '退款申请中' : '退货申请中';
        tip = '商家正在审核中，请耐心等待';
        isHaveCancel = true;
        break;
    case AFTER_ORDER_STATUS.WAITE_SEND_BACK:
        text = '待退货';
        tip = '请填写退货物流单号';
        isHaveCancel = true;
        isHaveExpress = true;
        break;
    case AFTER_ORDER_STATUS.USER_SENDED: // 用户寄出
        text = '待收货审核';
        tip = '请耐心等待商家处理';
        isHaveExpress = true;
        isShowExpress = true;
        break;
    case AFTER_ORDER_STATUS.BRAND_RECEIVED: // 商家确认收货
        text = '商家已确认收货';
        tip = '请耐心等待商家处理';
        isHaveExpress = true;
        isShowExpress = true;
        break;
    case AFTER_ORDER_STATUS.WAITE_PAY_BACK: // 商家确认收货
        text = '退款中';
        tip = '请注意查收退款消息';
        isHaveExpress = !isOnlyRefund;
        isShowExpress = !isOnlyRefund;
        break;
    case AFTER_ORDER_STATUS.COMPLATE:
        text = '售后已成功';
        tip = `已成功${isOnlyRefund ? '退款' : '退货退款'}，售后服务结束`;
        break;
    case AFTER_ORDER_STATUS.CLOSE:
        text = '售后已关闭';
        tip = '售后服务已完成，如有疑问请联系客服';
        tip = cancelled > 0 ? '您已取消退款申请，退款已关闭' : `${isOnlyRefund ? '退款' : '退货'}被拒，如有疑问请联系商家`;
        isHaveRefuseReason = cancelled > 0 ? false : true;
        break;
    default:
        text = '未知售后状态';
        tip = '刷新页面获取售后信息';
        break;
    }
    return {
        text,
        tip,
        isHaveCancel,
        isHaveExpress,
        isShowExpress,
        isHaveRefuseReason,
    };
};

/**
 * 处理品牌侧订单状态信息
 * @param {Object} orderInfo 订单详情
 */
export const handleBrandOrderStatus = (orderInfo) => {
    const { orderStatus, deliveryMode, isApplyForAfterSale, isInAfterSale } = orderInfo;
    let text = '订单状态异常';
    let tip = '订单状态异常，请刷新后尝试';
    let iconType = ''; // 状态展示图标
    let isHaveCancel = false; // 是否存在取消订单按钮
    let isHaveActionBar = false; // 是否存在操作栏
    let isHaveNotice = false; // 是否有缺货通知
    let isHaveConfirm = false; // 是否有确定按钮，确认发货、确认备货
    let isHaveComplate = false; // 是否有完成按钮，确认提货
    let isHaveExpressInfo = false; // 是否存在快递信息
    let isHavePickUpInfo = false; // 是否存在自提信息
    let isHavePickUpCode = false; // 是否存在自提码
    let isHaveAfterSale = false; // 是否存在正在售后的信息
    let isPickUp = deliveryMode === DELIVERY_TYPE_ENUM.self; // 是否是自提
    let isDayExpress = deliveryMode === DELIVERY_TYPE_ENUM.city; // 是否是当日达
    let isExpress = deliveryMode === DELIVERY_TYPE_ENUM.express; // 是否是快递
    switch (orderStatus) {
        case ORDER_STATUS.ORDER_CREATED:
            text = '订单已创建';
            tip = '10分钟内未付款，订单将取消';
            iconType = 'pay';
            break;
        case ORDER_STATUS.ORDER_TO_PAY:
            text = '待付款';
            tip = '货到付款，货物送达后收款';
            iconType = 'pay';
            break;
        case ORDER_STATUS.ORDER_NEED_PAY:
            text = '待付款';
            tip = '10分钟内未完成付款，订单将取消';
            iconType = 'pay';
            break;
        case ORDER_STATUS.ORDER_PAYED:
            text = isExpress ? '待发货' : '待备货';
            tip = isExpress ? '请尽快完成发货' : '请尽快完成备货';
            isHaveNotice = true;
            isHaveConfirm = true;
            isHaveActionBar = true;
            isHavePickUpInfo = isPickUp;
            isHaveExpressInfo = isExpress;
            isHaveAfterSale = isApplyForAfterSale || isInAfterSale;
            iconType = 'pack';
            break;
        case ORDER_STATUS.ORDER_DELIVERED:
            text = '待收货';
            tip = '商品已寄出，等待买家签收';
            iconType = 'package';
            isHaveExpressInfo = true;
            break;
        case ORDER_STATUS.ORDER_PREPARED:
            text = '待自提';
            tip = '商品已备货完成，等待买家自提';
            iconType = 'pickup';
            isHavePickUpInfo = true;
            isHaveActionBar = true;
            isHavePickUpCode = true;
            isHaveComplate = true;
            break;
        case ORDER_STATUS.ORDER_CANCELLING:
            text = '取消申请中';
            tip = '请等待商家审核';
            iconType = 'cancelling';
            isHavePickUpInfo = isPickUp;
            isHaveExpressInfo = isExpress;
            break;
        case ORDER_STATUS.ORDER_COMPLETE:
            text = '已完成';
            tip = '交易完成，已完成订单';
            iconType = 'complete';
            isHavePickUpInfo = isPickUp;
            isHaveExpressInfo = isExpress;
            break;
        case ORDER_STATUS.ORDER_CLOSE_BEFORE:
        case ORDER_STATUS.ORDER_CLOSE_AFTER:
            text = '订单已关闭';
            tip = '交易取消，已关闭订单';
            iconType = 'close';
            break;
    }
    return {
        text,
        tip,
        iconType,
        isHaveCancel,
        isHaveNotice,
        isHaveConfirm,
        isHaveComplate,
        isHaveActionBar,
        isHaveExpressInfo,
        isHavePickUpInfo,
        isHavePickUpCode,
        isHaveAfterSale,
        isPickUp,
        isDayExpress,
        isExpress,
    };
};

/**
 * 处理品牌侧售后订单状态信息
 * @param {Object} orderInfo 订单详情
 */
export const handleBrandAfterSaleOrderStatus = (orderInfo) => {
    const { afterSaleStatus, type, cancelled } = orderInfo;
    let text = '订单状态异常';
    let tip = '订单状态异常，请刷新后尝试';
    let isHaveDealAction = false; // 是否有处理按钮，拒绝、同意
    let isHaveActionBar = false;
    let isHaveExpressAction = false; // 是否有入库操作
    let isShowExpress = false;
    let isHaveRefuseReason = false;
    const isOnlyRefund = type === ORDER_AFTER_SALE_TYPE.onlyRefund;
    switch (afterSaleStatus) {
    case AFTER_ORDER_STATUS.APPLYING: // 申请中
        text = isOnlyRefund ? '退款申请中' : '退货申请中';
        tip = '请尽快审核';
        isHaveDealAction = true;
        isHaveActionBar = true;
        break;
    case AFTER_ORDER_STATUS.WAITE_SEND_BACK:
        text = '待退货';
        tip = '请耐心等待买家寄回商品';
        isShowExpress = true;
        break;
    case AFTER_ORDER_STATUS.USER_SENDED: // 用户寄出
        text = '待收货审核';
        tip = '请注意查收并尽快完成入库审核';
        isHaveExpressAction = true;
        isShowExpress = true;
        isHaveActionBar = true;
        break;
    case AFTER_ORDER_STATUS.BRAND_RECEIVED: // 商家确认收货
        text = '商家已确认收货';
        tip = '请尽快完成退款';
        isShowExpress = true;
        break;
    case AFTER_ORDER_STATUS.WAITE_PAY_BACK: // 退款中
        text = '退款中';
        tip = '请通知用户查收退款消息';
        isShowExpress = !isOnlyRefund;
        break;
    case AFTER_ORDER_STATUS.COMPLATE:
        text = '售后已成功';
        tip = `已成功${isOnlyRefund ? '退款' : '退货退款'}，售后服务结束`;
        break;
    case AFTER_ORDER_STATUS.CLOSE:
        text = '售后已关闭';
        tip = cancelled > 0 ? '买家已取消申请，售后服务已关闭' : `拒绝${isOnlyRefund ? '退款' : '退货'}，请及时与买家沟通`;
        isHaveRefuseReason = cancelled > 0 ? false : true;
        break;
    default:
        text = '未知售后状态';
        tip = '刷新页面获取售后信息';
        break;
    }
    return {
        text,
        tip,
        isHaveActionBar,
        isHaveDealAction,
        isHaveExpressAction,
        isShowExpress,
        isHaveRefuseReason,
    };
};